package wili.attrMapping._user.b1;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import wili.a.Refs;
import wili.attrMapping.core.Att9;
import wili.attrMapping.core.Atts;
import wili.attrMapping.core.AttsDefine;
import wili.attrMapping.core.AttsFile;
import wili.attrMapping.core.UserConf;
import wili.attrMapping.core.ValueMapingNode;
import wili.c.Ca;

/**
 * >>> 属性映射之规则约束。
 * a. generate template.
 * b. generate rule.
 * c. update target value.
 */
public class AttMapingRule_main implements AttConst {
	final List<String> errMsgs = new ArrayList<>();

	public static void main(String[] args) {
		AttMapingRule_main obj = new AttMapingRule_main();
		obj.start(1, new JsonEditableConf());
		obj.start(-1, new JsonVisibleConf());
		obj.start(-1, new XmlSalesConf());
	}

	public void start(int use, UserConf conf) {
		if (use < 0)
			return;
		String action = "genTemp,genRule,updateTargetValue".split(",")[//
		2//
		].intern();
		ValueMapingNode rule = null;
		AttsDefine def = conf.getAttsDefine();
		List<File> oldFiles = conf.getOldFiles();
		//
		for (int i = 0; i < oldFiles.size(); i++) {
			AttsFile oldFile = AttsFile.newInst(oldFiles.get(i));
			List<Atts> oldAttsList = conf.findOldAttsList(def, oldFile);
			if (Boolean.FALSE) {
			} else if (action == AttAct.genTemp) {// collect old attrs, then gen template.
				if (rule == null)
					rule = new ValueMapingNode(def.objName);
				Refs.useAttsBean();
				conf.genRulesOf_oldAttsList(true, oldAttsList, rule);//
			} else if (action == AttAct.genRule) {// collect old attrs ~>new attrs mapping, then generate rule table.
				if (rule == null)
					rule = new ValueMapingNode(def.objName);
				rule = conf.genRulesOf_oldAttsList(false, oldAttsList, rule);//
			} else if (action == AttAct.update) {// update target file.
				if (rule == null)
					rule = conf.getMapingRule(rule);
				Set<AttsFile> fileSet;
				fileSet = conf.valueOfNewAtts_inOldFile(false, oldFile, def, rule, this);//
				if (fileSet.isEmpty())
					return;
				int j = 0;
				for (AttsFile ele : fileSet) {
					int[] times = ele.getUpdateTimes();
					ele.saveToFile(null, (fileSt) -> conf.replaceFileContent_atLast(fileSt));
					++j;
				}
			} else {
				Ca.asert(false, "action=" + action);
			}
		}
		if (Boolean.FALSE) {
		} else if (action == AttAct.genTemp) {
			Ca.log(1, "\n>>> template generated:");
			ValueMapingNode.PRINT_TEMPLATE = true;
			rule.print(1);
		} else if (action == AttAct.update) {
			Att9.printMsgs();
		} else if (Boolean.FALSE && action == AttAct.check) {
			Att9.debug("\n>>> Number of errors:" + errMsgs.size());
		} else if (action == AttAct.genRule) {
			Att9.debug("\n>>> rule generated:"
					+ oldFiles.stream().map(file -> file.getName()).collect(Collectors.toList()));
			Ca.log("\n=============generated rule:");
			rule.print(1);
			Ca.log("\n=============generated rule:end.\nInconsistentTimes=" + ValueMapingNode.inconsistentTimes);
			Att9.debug("nodeNum=", rule.nodeNum(true));
		}
		Att9.debug("\nscanNum=" + scanNum + ", oldNum=" + oldNum + ", newNum=" + newNum + ", bugNum=" + bugNum);
	}

	public static int oldNum, newNum;

	public static int scanNum, bugNum;

	private AttMapingRule_main() {

	}

}
